package com.inspur_video.inspur_video.controller;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.inspur_video.inspur_video.bean.video.Video;
import com.inspur_video.inspur_video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-08-11 09:46
 * @package com.inspur_video.inspur_video.controller
 * @description
 */
@Controller
@RequestMapping("/video_back")
public class VideoBackController {
    @Autowired
    VideoService videoService;
    @Autowired
    HttpSession httpSession;
    @RequestMapping("/list")
    public String getList(@RequestParam(required = false,defaultValue = "0") int num,
                          @RequestParam(required = false,defaultValue = "12") int size, Model model){
        Page<Video> videoPage = videoService.getVideoPage(num, size);
        model.addAttribute("page",videoPage);
        return "look";
    }

    @RequestMapping("/update")
    public String updateList(@RequestParam(required = false,defaultValue = "0") int num,
                          @RequestParam(required = false,defaultValue = "12") int size, Model model){
        Page<Video> videoPage = videoService.getVideoPage(num, size);
        model.addAttribute("page",videoPage);
        return "update";
    }

    @RequestMapping("/play")
    public String videoPlay(@RequestParam(required = false,defaultValue = "ali")String definition, String aId,Model model){
        System.out.println(aId);
        int id=Integer.parseInt(httpSession.getAttribute("user").toString());
        List<GetPlayInfoResponse.PlayInfo> list = videoService.getALiVideoPlay(definition, aId, id);
        model.addAttribute("videoURL",list);
        Video video=videoService.getVideoInfoByAId(aId);
        model.addAttribute("video",video);
        return "play";
    }

    /**
     * 模糊查询（包括三个字段的模糊查询）
     * @param select
     * @param input
     * @param num
     * @param size
     * @param model
     * @return
     */
    @RequestMapping("/find_like")
    public String findByTitleAndDescribeLike(String select,String input,
                                                  @RequestParam(required = false,defaultValue = "0") int num,
                                                  @RequestParam(required = false,defaultValue = "12") int size,Model model) {
        String describe="",tags="",title="";
        if ("视频标题".equals(select)){
            System.out.println("视频标题"+input);
            title=input;


        }
        if ("视频标签".equals(select)){
            System.out.println("视频标签"+input);
            tags=input;

        }
        if ("视频描述".equals(select)){
            System.out.println("视频描述"+input);
            describe=input;

        }
        Page<Video> page = videoService.findByTitleAndDescribeLikePage(title, describe, tags, num, size);
        model.addAttribute("page",page);
        model.addAttribute("searchWords",input);
        model.addAttribute("searchSelect",select);
        return "update";
    }

    /**
     * 模糊查询（包括三个字段的模糊查询）
     * @param select
     * @param input
     * @param num
     * @param size
     * @param model
     * @return
     */
    @RequestMapping("/find_like_look")
    public String findByTitleAndDescribeLikeLook(String select,String input,
                                             @RequestParam(required = false,defaultValue = "0") int num,
                                             @RequestParam(required = false,defaultValue = "12") int size,Model model) {
        String describe="",tags="",title="";
        if ("视频标题".equals(select)){
            System.out.println("视频标题"+input);
            title=input;


        }
        if ("视频标签".equals(select)){
            System.out.println("视频标签"+input);
            tags=input;

        }
        if ("视频描述".equals(select)){
            System.out.println("视频描述"+input);
            describe=input;

        }
        Page<Video> page = videoService.findByTitleAndDescribeLikePage(title, describe, tags, num, size);
        model.addAttribute("page",page);
        model.addAttribute("searchWords",input);
        model.addAttribute("searchSelect",select);
        return "look";
    }
}
